<?php 

function request($symbol, $stat) {
   $url = 'http://finance.yahoo.com/d/quotes.csv?s=' . $symbol . '&f=' . $stat;    
   $ret = file_get_contents($url);
   return $ret;
}

function get_all($symbol) {
    /*
    Get all available quote data for the given ticker symbol.
    
    Returns a dictionary.
    */
    $values_raw = request($symbol, 'l1c1va2xj1b4j4dyekjm3m4rr5p5p6s7');
    $values = explode(",", $values_raw);
    $data = array();
    $data['price'] = $values[0];
    $data['change'] = $values[1];
    $data['volume'] = $values[2];
    $data['avg_daily_volume'] = $values[3];
    $data['stock_exchange'] = $values[4];
    $data['market_cap'] = $values[5];
    $data['book_value'] = $values[6];
    $data['ebitda'] = $values[7];
    $data['dividend_per_share'] = $values[8];
    $data['dividend_yield'] = $values[9];
    $data['earnings_per_share'] = $values[10];
    $data['52_week_high'] = $values[11];
    $data['52_week_low'] = $values[12];
    $data['50day_moving_avg'] = $values[13];
    $data['200day_moving_avg'] = $values[14];
    $data['price_earnings_ratio'] = $values[15];
    $data['price_earnings_growth_ratio'] = $values[16];
    $data['price_sales_ratio'] = $values[17];
    $data['price_book_ratio'] = $values[18];
    $data['short_ratio'] = $values[19];
    return $data;
}    
    
function get_price($symbol) { 
    return request($symbol, 'l1');
}

function get_change($symbol) {
    return request($symbol, 'c1');
}   
    
function get_volume($symbol) { 
    return request($symbol, 'v');
}

function get_avg_daily_volume($symbol) { 
    return request($symbol, 'a2');
}   
    
function get_stock_exchange($symbol) { 
    return request($symbol, 'x');
}    
    
function get_market_cap($symbol) {
    return request($symbol, 'j1');
}   
   
function get_book_value($symbol) {
    return request($symbol, 'b4');
}

function get_ebitda($symbol) { 
    return request($symbol, 'j4');
}    
    
function get_dividend_per_share($symbol) {
    return request($symbol, 'd');
}

function get_dividend_yield($symbol) { 
    return request($symbol, 'y');
}    
    
function get_earnings_per_share($symbol) { 
    return request($symbol, 'e');
}

function get_52_week_high($symbol) { 
    return request($symbol, 'k');
}    
    
function get_52_week_low($symbol) { 
    return request($symbol, 'j');
}

function get_50day_moving_avg($symbol) { 
    return request($symbol, 'm3');
}    
    
function get_200day_moving_avg($symbol) { 
    return request($symbol, 'm4');
}    
    
function get_price_earnings_ratio($symbol) { 
    return request($symbol, 'r');
}

function get_price_earnings_growth_ratio($symbol) { 
    return request($symbol, 'r5');
}

function get_price_sales_ratio($symbol) { 
    return request($symbol, 'p5');
}   
    
function get_price_book_ratio($symbol) { 
    return request($symbol, 'p6');
}       
       
function get_short_ratio($symbol) { 
    return request($symbol, 's7');
}
    
/*
function get_historical_prices($symbol, $start_date, $end_date) {
    /*
    Get historical prices for the given ticker symbol.
    Date format is 'YYYYMMDD'
    
    Returns a nested list.
   */
  /*  $url = 'http://ichart.yahoo.com/table.csv?s=' . $symbol . '&' .
          'd=' . str(int(end_date[4:6]) - 1) . '&' .
          'e=' . str(int(end_date[6:8])) . '&' .
          'f=' . str(int(end_date[0:4])) . '&' .
          'g=d&' + \
          'a=' . str(int(start_date[4:6]) - 1). '&' .
          'b=' . str(int(start_date[6:8])) . '&' .
          'c=%s&' % str(int(start_date[0:4])) . 
          'ignore=.csv';
    
    $days = urllib.urlopen(url).readlines()
    $data = [day[:-2].split(',') for day in days]
    
    return data;
}
*/

?>